//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "CallableBond.h"
using namespace Cephei::QL::Experimental::Callablebonds;
#include <gen/QL/Instruments/Callability.h>
#include <gen/QL/Termstructures/YieldTermStructure.h>
#include <gen/QL/Times/Calendar.h>
#include <gen/QL/CashFlow.h>
#include <gen/QL/Times/DayCounter.h>
#include <gen/QL/PricingEngine.h>
#include <gen/QL/Instruments/Bond.h>
using namespace Cephei::QL::Instruments;
using namespace Cephei::QL::Termstructures;
using namespace Cephei::QL::Times;
using namespace Cephei::QL;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Experimental::Callablebonds::CCallableBond::CCallableBond (boost::shared_ptr<QuantLib::CallableBond>& childNative, Object^ owner) : CBond(CCallableBond::typeid)
{
#ifdef HANDLE
	_phCallableBond = NULL;
#endif
	_ppCallableBond = &childNative;
    _ppBond = new boost::shared_ptr<QuantLib::Bond> (boost::dynamic_pointer_cast<QuantLib::Bond> (*_ppCallableBond));
}
Cephei::QL::Experimental::Callablebonds::CCallableBond::CCallableBond (QuantLib::CallableBond& childNative, Object^ owner) : CBond(CCallableBond::typeid)
{
#ifdef HANDLE
	_phCallableBond = NULL;
#endif
	_ppCallableBond = new boost::shared_ptr<QuantLib::CallableBond> (&childNative);
    _ppBond = new boost::shared_ptr<QuantLib::Bond> (boost::dynamic_pointer_cast<QuantLib::Bond> (*_ppCallableBond));
    _CallableBondOwner = owner;
    _BondOwner = owner;
}

Cephei::QL::Experimental::Callablebonds::CCallableBond::CCallableBond (CCallableBond^ copy) : CBond(CCallableBond::typeid)
{
#ifdef HANDLE
	_phCallableBond = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppCallableBond = new boost::shared_ptr<QuantLib::CallableBond> (copy->GetShared());
        _ppBond = new boost::shared_ptr<QuantLib::Bond> (boost::dynamic_pointer_cast<QuantLib::Bond> (*_ppCallableBond));
    }
}
Cephei::QL::Experimental::Callablebonds::CCallableBond::CCallableBond (System::Type^ t) : CBond(CCallableBond::typeid)
{
#ifdef HANDLE
	_phCallableBond = NULL;
#endif
	if (!t->IsSubclassOf(CCallableBond::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Experimental::Callablebonds::CCallableBond::CCallableBond (QuantLib::Handle<QuantLib::CallableBond>& childNative, Object^ owner)  : CBond(CCallableBond::typeid)
{
	_phCallableBond = &childNative;
	_ppCallableBond = &static_cast<boost::shared_ptr<QuantLib::CallableBond>>(childNative.currentLink());
    _ppBond = new boost::shared_ptr<QuantLib::Bond> (boost::dynamic_pointer_cast<QuantLib::Bond> (*_ppCallableBond));
    _CallableBondOwner = owner;
}
Cephei::QL::Experimental::Callablebonds::CCallableBond::CCallableBond (QuantLib::Handle<QuantLib::CallableBond> childNative)  : CBond(CCallableBond::typeid)
{
	_phCallableBond = &childNative;
	_ppCallableBond = &static_cast<boost::shared_ptr<QuantLib::CallableBond>>(childNative.currentLink());
    _ppBond = new boost::shared_ptr<QuantLib::Bond> (boost::dynamic_pointer_cast<QuantLib::Bond> (*_ppCallableBond));
}
#endif
#ifdef STRUCT
Cephei::QL::Experimental::Callablebonds::CCallableBond::CCallableBond (QuantLib::CallableBond childNative)  : CBond(CCallableBond::typeid)
{
#ifdef HANDLE
	_phCallableBond = NULL;
#endif
	_ppCallableBond = new boost::shared_ptr<QuantLib::CallableBond> (new QuantLib::CallableBond (childNative));
    _ppBond = new boost::shared_ptr<QuantLib::Bond> (boost::dynamic_pointer_cast<QuantLib::Bond> (*_ppCallableBond));
}
#endif

Cephei::QL::Experimental::Callablebonds::CCallableBond::~CCallableBond ()
{
    if (_ppCallableBond != NULL)
    {
	    delete _ppCallableBond;
        _ppCallableBond = NULL;
    }
}
Cephei::QL::Experimental::Callablebonds::CCallableBond::!CCallableBond ()
{
    if (_ppCallableBond != NULL)
    {
	    delete _ppCallableBond;
    }
}
QuantLib::CallableBond& Cephei::QL::Experimental::Callablebonds::CCallableBond::GetReference ()
{
    if (_ppCallableBond == NULL) throw gcnew NativeNullException ();
	return **_ppCallableBond;
}
boost::shared_ptr<QuantLib::CallableBond>& Cephei::QL::Experimental::Callablebonds::CCallableBond::GetShared ()
{
    if (_ppCallableBond == NULL) throw gcnew NativeNullException ();
	return *_ppCallableBond;
}
QuantLib::CallableBond* Cephei::QL::Experimental::Callablebonds::CCallableBond::GetPointer ()
{
    if (_ppCallableBond == NULL) throw gcnew NativeNullException ();
	return &**_ppCallableBond;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::CallableBond>& Cephei::QL::Experimental::Callablebonds::CCallableBond::GetHandle ()
{
	if (_phCallableBond == NULL)
	{
		_phCallableBond = new Handle<QuantLib::CallableBond> (*_ppCallableBond);
	}
	return *_phCallableBond;
}
#endif
bool Cephei::QL::Experimental::Callablebonds::CCallableBond::HasNative () 
{
	return (_ppCallableBond != NULL);
}

Cephei::IVector<Cephei::QL::Instruments::ICallability^>^ Cephei::QL::Experimental::Callablebonds::CCallableBond::Callability::get ()
{
    try
    {
    	std::vector<boost::shared_ptr<QuantLib::Callability> >& _rv = (std::vector<boost::shared_ptr<QuantLib::Callability> >&)(*_ppCallableBond)->callability ( );   
        CoVector<Cephei::QL::Instruments::ICallability^>^ _nrv = gcnew CoVector<Cephei::QL::Instruments::ICallability^>(gcnew CCallabilityVector (_rv, this));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Experimental::Callablebonds::CCallableBond::ImpliedVolatility (Double targetValue, Cephei::QL::Termstructures::IYieldTermStructure^ discountCurve, Double accuracy, UInt64 maxEvaluations, Double minVol, Double maxVol)
{
    CYieldTermStructure^ _CdiscountCurve;
    try
    {
        QuantLib::Real _targetValue = (QuantLib::Real)ValueHelper::Convert (targetValue);
        _CdiscountCurve = safe_cast<CYieldTermStructure^> (discountCurve);
        _CdiscountCurve->Lock();
        Handle<QuantLib::YieldTermStructure>& _discountCurve = static_cast<Handle<QuantLib::YieldTermStructure>&> (_CdiscountCurve->GetHandle ()); 
        QuantLib::Real _accuracy = (QuantLib::Real)ValueHelper::Convert (accuracy);
        QuantLib::Size _maxEvaluations = (QuantLib::Size)ValueHelper::Convert (maxEvaluations);
        QuantLib::Volatility _minVol = (QuantLib::Volatility)ValueHelper::Convert (minVol);
        QuantLib::Volatility _maxVol = (QuantLib::Volatility)ValueHelper::Convert (maxVol);
    	QuantLib::Volatility _rv = (QuantLib::Volatility)(*_ppCallableBond)->impliedVolatility ( _targetValue,  _discountCurve,  _accuracy,  _maxEvaluations,  _minVol,  _maxVol );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_CdiscountCurve != nullptr) _CdiscountCurve->Unlock();
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

